/*
 * @Author: bor_fish 1219854025@qq.com
 * @Date: 2025-04-24 20:00:30
 * @LastEditors: bor_fish 1219854025@qq.com
 * @LastEditTime: 2025-05-15 15:24:06
 * @FilePath: /myCAN/Diagnostic2.cpp
*/
#include "Diagnostic2.h"
#include <iostream>
void Diagnostic2::parseData() {
    //std::cout << "Diagnostic2 Running!" << std::endl;
    // 线程安全帧对象
    can_frame frame;
    // CAN接口索引
    int interface_index;
    int last_processed_index = 0;
    
    while(isRunning()) {
        // 获取新数据
        if (listener_.getNextData(frame, interface_index, thread_Index_)) {
            //std::cout << "Diagnostic2 processing data from interface " << interface_index << std::endl;
            // 检查是否需要处理这个数据
        int current_write_index = listener_.getWriteIndex(interface_index);
            
        // 处理从上次处理位置到当前位置的所有数据
        for (int i = last_processed_index; i < current_write_index; i++) {

            int buffer_index = i % listener_.getBufferSize();
    
            // 从packet_Buffer_中提取数据
            auto& packet = listener_.getPacketBuffer()[interface_index][buffer_index];
    
            // 提取时间戳
            int64_t timestamp;
            std::memcpy(&timestamp, &packet[2], sizeof(int64_t));
    
            // 提取帧ID
            uint32_t frame_id;
            std::memcpy(&frame_id, &packet[21], sizeof(uint32_t));
    
            // 处理数据
            if (frame_id == 0x456) {  // 假设诊断1处理ID为0x123的帧
              
            }
        }

            // 通知数据已处理
            listener_.notifyDataProcessed();
        }
    }
}